package pl.register.android.activities.correlation.builder;

import android.app.DatePickerDialog;
import android.content.Context;
import android.text.format.Time;
import android.widget.DatePicker;
import pl.register.android.util.time.DateUtil;

import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author Mateusz Strzelecki
 * @since 2013-05-01
 */
public abstract class DateDialogBuilder {

    private DatePickerDialog dateDlg;

    protected abstract void settleNewDate(Date newDate);

    public DateDialogBuilder(Context context, Date date, String message) {
        GregorianCalendar calendar = new GregorianCalendar();
        if (date != null) {
            calendar.setTime(date);
        } else {
            calendar.setTime(new Date());
        }

        dateDlg = new DatePickerDialog(context,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        Time chosenDate = new Time();
                        chosenDate.set(dayOfMonth, monthOfYear, year);
                        Date newDate = new Date(chosenDate.toMillis(true));
                        newDate = DateUtil.resetTime(newDate);
                        settleNewDate(newDate);
                    }
                }, calendar.get(GregorianCalendar.YEAR), calendar.get(GregorianCalendar.MONTH), calendar.get(GregorianCalendar.DAY_OF_MONTH)
        );

        dateDlg.setMessage(message);

    }

    public void show() {
        dateDlg.show();
    }
}